<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Installation &amp; configuration of Derby</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Apache Derby, Java, tutorial, database, installation, configuration">
<meta name="description" content="In this chapter of the Apache Derby tutorial,
we cover the installation &amp; configuration of Derby.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Installation &amp; configuration of Derby</h1>

<p>
In the following pages, we will show how to install Derby and
will configure Derby to our needs. 
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 


<p>
We download the latest version of Derby from
<a href="http://db.apache.org/derby/">db.apache.org/derby/</a>. 
We create an installation directory and unpack the compressed files
to this directory. 
</p>

<pre>
$ ls
bin   docs        javadoc  lib      NOTICE              test
demo  index.html  KEYS     LICENSE  RELEASE-NOTES.html
</pre>

<p>
We list the contents of the installation directory. In the bin subdirectory,
we have several Derby tools. The docs directory provides documentation for
Derby. In HTML and PDF format. The documentation is very good and we highly
recommend to have a look at these files. In the lib subdirectory there
are various JAR files needed for working with Derby.
</p>


<h2>The Java DB</h2>

<p>
Derby is included in the JDK since Java 6. Java DB is a supported version 
of Apache Derby and contains the same binaries as Apache Derby.
</p>

<pre>
$ ls jdk1.6.0_30/db
3RDPARTY  demo  index.html  lib      NOTICE         RELEASE-NOTES.html
bin       docs  javadoc     LICENSE  register.html
</pre>

<p>
In the JDK installation directory, we have a subdirectory called db, where
we find the Java DB files. 
</p>

<p>
In the Overview section of the release notes we read the following:
</p>

<p>
Derby is a pure Java relational database engine using standard SQL and JDBC as its APIs.
</p>

<p>
Derby functionality includes:
</p>

<ul>
    <li>Embedded engine with JDBC drivers</li>
    <li>Network Server</li>
    <li>Network client JDBC drivers</li>
    <li>Command line tools: ij (SQL scripting), dblook (schema dump) and sysinfo (system info)</li>
</ul>


<h2>Environment variables</h2>

<p>
Setting environment variables is optional. Using environment variables will make the
life easier for us.
</p>

<pre>
$ export DERBY_HOME=~/bin/derby
$ export PATH=$PATH:~/bin/derby/bin
$ export DERBY_HOME=-Dderby.system.home=/home/janbodnar/programming/derby/dbs
</pre>

<p>
We set a DERBY_HOME variable to the Derby installation directory. Some tools may use
this variable. Then we add a bin subdirectory to the PATH variable. This way we
do not have to fully specify the path to the Derby tools. In the DERBY_OPTS environment
variable we can set various Derby or Java related options. For example, we set the
derby.system.home property. 
</p>

<pre>
$ java -jar $DERBY_HOME/lib/derbyrun.jar sysinfo
------------------ Java Information ------------------
Java Version:    1.6.0_30
Java Vendor:     Sun Microsystems Inc.
Java home:       /home/janbodnar/bin/jdk1.6.0_30/jre
Java classpath:  /home/janbodnar/bin/derby/lib/derbyrun.jar
OS name:         Linux
OS architecture: i386
...
</pre>

<p>
We use the derbyrun.jar file to execute the sysinfo tool to get some
info about the Java and Derby. 
</p>


<h2>Creating a database</h2>

<p>
Derby does not have a <code>CREATE DATABASE</code> statement like MySQL or Oracle.
We have to create a database by creating a connection and setting a connection
property create=true.
</p>

<pre>
$ java -Dderby.system.home=/home/janbodnar/programming/derby/dbs \
> -jar $DERBY_HOME/lib/derbyrun.jar ij

ij version 10.8
ij>
</pre>

<p>
We start the ij tool with the derbyrun.jar file. We set the derby system directory. It is the directory,
where we create databases and where log file is created. 
</p>

<pre>
ij> connect 'jdbc:derby:tmpdb;user=tmpuser;create=true';
</pre>

<p>
A database tmpdb is created inside the Derby system directory and we 
also connect to it. The connection is created in the embedded mode. 
</p>

<pre>
ij> CREATE TABLE FRIENDS(ID INT PRIMARY KEY, NAME VARCHAR(25));
0 rows inserted/updated/deleted
ij> INSERT INTO FRIENDS(ID, NAME) VALUES(1, 'Jane');
1 row inserted/updated/deleted
ij> INSERT INTO FRIENDS(ID, NAME) VALUES(2, 'Thomas');
1 row inserted/updated/deleted
ij> INSERT INTO FRIENDS(ID, NAME) VALUES(3, 'Beky');
1 row inserted/updated/deleted
</pre>

<p>
We create a FRIENDS table and add three rows to it. 
</p>

<pre>
ij> SELECT * FROM FRIENDS;
ID         |NAME                     
-------------------------------------
1          |Jane                     
2          |Thomas                   
3          |Beky                     

3 rows selected
</pre>

<p>
We check the data. 
</p>

<pre>
ij> SHOW TABLES IN TMPUSER;
TABLE_SCHEM         |TABLE_NAME                    |REMARKS             
------------------------------------------------------------------------
TMPUSER             |FRIENDS                       |                    

1 row selected
</pre>

<p>
When we created a connection, we have specified a user name. The user
name is the database schema, in which the FRIENDS table is created.
</p>

<pre>
ij> DISCONNECT;
ij> SHOW CONNECTIONS;
No connections available.
ij> EXIT;
$ 
</pre>

<p>
We disconnect from the tmpdb database. The <code>SHOW CONNECTIONS</code>
statement informs that there are no open connections. We exit the ij
tool with the EXIT command. 
</p>

<pre>
$ rm -rf dbs/tmpdb
</pre>

<p>
We delete the database from the Derby system. Derby has no <code>DROP DATABASE</code>
statement. 
</p>

<p>
This is the end of the Derby tutorial chapter, in which we have installed and configured Derby.
</p>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 3, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

